package com.android.tea;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.android.tea.api.ApiResult;
import com.android.tea.api.ApiReturnResultListener;
import com.android.tea.api.TeaApi;
import com.android.tea.util.StringUtils;
import com.android.tea.util.ToastManager;

public class FeedBackActivity extends Activity implements OnClickListener{
	private TextView btn_back;
	private TextView btn_send;
	private EditText content_edit;
	private EditText username_edit;
	private EditText telephone_edit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feedback);
		initComponents();
	}

	private void initComponents() {
		btn_back = (TextView)findViewById(R.id.btn_back);
		btn_send = (TextView)findViewById(R.id.btn_send);
		btn_back.setOnClickListener(this);
		btn_send.setOnClickListener(this);
		
		content_edit = (EditText)findViewById(R.id.feed_content);
		username_edit = (EditText)findViewById(R.id.username);
		telephone_edit = (EditText)findViewById(R.id.telephone);
		
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();
			break;
		case R.id.btn_send:
			String content = content_edit.getText().toString();
			String username = username_edit.getText().toString();
			String telephone = telephone_edit.getText().toString();
			if(StringUtils.isNullOrEmpty(content)) {
				ToastManager.showToast(this, R.string.account);
				return;
			}
			if(StringUtils.isNullOrEmpty(username)) {
				ToastManager.showToast(this, R.string.account);
				return;
			}
			if(StringUtils.isNullOrEmpty(telephone)) {
				ToastManager.showToast(this, R.string.account);
				return;
			}
			TeaApi api = new TeaApi(this);
			api.feedBack(1,content,username,telephone);
			api.setReturnResultListener(new ApiReturnResultListener() {
				
				@Override
				public <T> void onReturnSucceedResult(int requestCode,
						ApiResult<T> apiResult) {
					if(ApiResult.RESULT_OK==apiResult.getResultCode()) {
						String msg = apiResult.getResultMsg();
						if(!StringUtils.isNullOrEmpty(msg)) {
							try {
								int result = Integer.parseInt(msg);
								if(1==result) {
									ToastManager.showToast(FeedBackActivity.this, R.string.send_success);
								}else {
									ToastManager.showToast(FeedBackActivity.this, R.string.send_fail);
								}
							} catch (NumberFormatException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
				
				@Override
				public <T> void onReturnFailResult(int requestCode, ApiResult<T> apiResult) {
					// TODO Auto-generated method stub
					
				}
			});
			break;

		default:
			break;
		}
	}

}
